//
//  PushTabbar.swift
//  iOSProjectExtensionSwift
//
//  Created by HBGG on 2020/8/29.
//  Copyright © 2020 lidianchao. All rights reserved.
//

import UIKit

class PushTabbar: UITabBar {
    var oldSafeAreaInsets = UIEdgeInsets()
    override init(frame: CGRect) {
        super.init(frame: frame)
        oldSafeAreaInsets = .zero
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func safeAreaInsetsDidChange() {
        super.safeAreaInsetsDidChange()
        if(oldSafeAreaInsets != self.safeAreaInsets) {
            self.invalidateIntrinsicContentSize()
            guard self.superview == nil else {
                self.superview?.setNeedsLayout()
                self.superview?.layoutSubviews()
                return
            }
            
        }
    }
    override func sizeThatFits(_ size: CGSize) -> CGSize {
        var currentSize = super.sizeThatFits(size)
        if #available(iOS 11, *) {
            let bottomInset = self.safeAreaInsets.bottom
            if(bottomInset > 0 && currentSize.height < 50 && (currentSize.height + bottomInset < 90)) {
                currentSize.height += bottomInset
            }
        }
        return currentSize
    }
    override var frame: CGRect {
        didSet {
            guard self.superview != nil else {
                return
            }
            var newFrame = frame
            if (frame.origin.y + frame.size.height != self.superview?.frame.size.height) {
                newFrame.origin.y = (self.superview?.frame.size.height)! - newFrame.size.height;
            }
            super.frame = newFrame
        }
    }
    
    
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
